package com.master.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URI;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HttpUtil {

    public static JSONObject doGet(String url, Map<String, Object> params) {
        //判断传入的url是否需要拼接http://前缀
        if (!url.contains("http://")) {
            url = "http://" + url;
        }

        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        JSONObject result = null;
        CloseableHttpResponse response = null;
        try {
            //创建URI
            URIBuilder builder = new URIBuilder(url);
            if (params != null) {
                for (String key : params.keySet()) {
                    builder.addParameter(key, String.valueOf(params.get(key)));
                }
            }
            URI uri = builder.build();

            //创建http get请求
            HttpGet get = new HttpGet(uri);

            //执行请求
            response = httpClient.execute(get);
            //判断返回状态
            if (response.getStatusLine().getStatusCode() == 200) {
                result = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                httpClient.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    private static JSONObject doGet(String url) {
        //判断传入的url是否需要拼接http://前缀
        if (!url.contains("http://")) {
            url = "http://" + url;
        }
        return doGet(url, null);
    }

    public static JSONObject doPost(String url, Map<String, Object> params) {
        //判断传入的url是否需要拼接http://前缀
        if (!url.contains("http://")) {
            url = "http://" + url;
        }
        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        JSONObject result = null;

        //创建post请求
        HttpPost post = new HttpPost(url);
        post.addHeader("Content-Type", "application/x-www-form-urlencoded");

        try {
            //创建参数列表
            if (params != null) {
                List<NameValuePair> paramList = new ArrayList<>();
                for (String key : params.keySet()) {
                    paramList.add(new BasicNameValuePair(key, (String) params.get(key)));
                }
                //模拟表单
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList, "UTF-8");
                post.setEntity(entity);
            }
            //执行http请求
            response = httpClient.execute(post);
            result = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //关闭response对象和http对象
        try {
            assert response != null;
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public static JSONObject doPost(String url) {
        //判断传入的url是否需要拼接http://前缀
        if (!url.contains("http://")) {
            url = "http://" + url;
        }
        return doPost(url, null);
    }

    public static JSONObject doPostJson(String url, String json) {
        //判断传入的url是否需要拼接http://前缀
        if (!url.contains("http://")) {
            url = "http://" + url;
        }
        //创建httpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        JSONObject result = null;

        //创建post请求
        HttpPost post = new HttpPost(url);
        //创建请求内容
        StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
        post.setEntity(entity);

        //执行http请求
        try {
            response = httpClient.execute(post);
            result = JSONObject.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        //关闭http和response
        try {
            assert response != null;
            response.close();
            httpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

}
